/*
 * Copyright 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.androiddevchallenge.ui.theme

import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Divider
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Scaffold
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.material.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.example.androiddevchallenge.R
import com.example.androiddevchallenge.bean.PetBean

// Start building your app here!
// @OptIn(ExperimentalMaterialApi::class)
@Composable
fun HomeScreen(data: ArrayList<PetBean>, onItemClick: (item: PetBean) -> Unit) {
    Surface(color = MaterialTheme.colors.background) {
        Scaffold(
            topBar = {
                TopAppBar(
                    title = { Text(text = stringResource(R.string.pets_adoption)) },
                    navigationIcon = {
                        IconButton(onClick = { }) {
                            Icon(
                                painter = painterResource(R.drawable.ic_dog),
                                contentDescription = stringResource(R.string.pets_adoption)
                            )
                        }
                    }
                )
            },
            content = {
                PostList(data) { item -> onItemClick(item) }
            }
        )
    }
}

@Composable
private fun PostList(data: ArrayList<PetBean>, onItemClick: (item: PetBean) -> Unit) {
    LazyColumn(modifier = Modifier.padding(0.dp)) {
        items(data) { topic ->
            TopicItem(topic) { item -> onItemClick(item) }
            ItemsDivider()
        }
    }
}

/**
 * Display a full-width topic item
 *
 */
@Composable
private fun TopicItem(petBean: PetBean, onItemClick: (item: PetBean) -> Unit) {
    val image = painterResource(petBean.img)
    Row(
        modifier = Modifier
            .padding()
            .clickable { onItemClick(petBean) }
    ) {
        Image(
            painter = image,
            contentDescription = null, // decorative
            modifier = Modifier
                .padding(start = 12.dp, top = 6.dp, bottom = 6.dp)
                .align(Alignment.CenterVertically)
                .size(90.dp, 80.dp)
                .clip(RoundedCornerShape(4.dp))
        )
        Column(
            modifier = Modifier
                .align(Alignment.CenterVertically)
                .padding(start = 16.dp, end = 16.dp)
        ) {
            Text(
                text = petBean.title,
                style = MaterialTheme.typography.h6,
                maxLines = 1

            )
            Spacer(Modifier.height(8.dp))
            Text(
                text = petBean.content,
                color = Color.Gray,
                style = MaterialTheme.typography.body1,
                maxLines = 2
            )
        }
        Spacer(Modifier.weight(1f))
    }
}

/**
 * Full-width divider for topics
 */
@Composable
private fun ItemsDivider() {
    Divider(
        modifier = Modifier.padding(start = 72.dp),
        color = MaterialTheme.colors.onSurface.copy(alpha = 0.08f)
    )
}
